﻿using AutoMapper;
using B.S.Orders.Domain;
using B.S.Orders.ErrorCord;
using B.S.Orders.Write.Api.Application.Command.DispatchProductCommand;
using MediatR;
using SqlSugar;

namespace B.S.Orders.Write.Api.Application.CommandHander.DispatchProductCommandHandler
{
    public class UpdateDispatchProductCommandHandler : IRequestHandler<UpdateDispatchProductCommand, APIResult<object>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public UpdateDispatchProductCommandHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(UpdateDispatchProductCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code = APIEnum.success;
            result.Msg = "修改成功";

            var model = db.Queryable<DispatchProduct>().Where(p => p.DispatchProductId == request.dispatchProductId);
            if (model.Count() != 0)
            {
                var production = mapper.Map<DispatchProduct>(request);
                await db.Updateable<DispatchProduct>(production).ExecuteCommandAsync();
            }

            return await Task.FromResult(result);
        }
    }
}
